package com.example.day2.adapter;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.day2.R;
import com.example.day2.db.StoreVideoDap;
import com.example.day2.entity.VideoResult;
import com.example.day2.mvvm.utils.DBUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

public class VideoAdapter extends BaseMultiItemQuickAdapter<VideoResult.DataBean, BaseViewHolder> {
    public VideoAdapter() {
        //1直播0,5普通2全屏
        addItemType(0, R.layout.item_vode_normal);
        addItemType(1, R.layout.item_vode_live);
        addItemType(2, R.layout.item_vode_full);
        addItemType(5, R.layout.item_vode_normal);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, VideoResult.DataBean dataBean) {
        StandardGSYVideoPlayer player = baseViewHolder.getView(R.id.play);
        player.getBackButton().setVisibility(View.GONE);//隐藏返回键
        player.setAutoFullWithSize(true);//自动选择竖屏还是横屏
        player.setUp(dataBean.getVideopath(),true,null);



        baseViewHolder.setText(R.id.address,dataBean.getAddress());
        baseViewHolder.setText(R.id.author, dataBean.getAuthname());
        baseViewHolder.setText(R.id.caption,dataBean.getCaption());

        baseViewHolder.setText(R.id.pao,dataBean.getAuthname()+"的原创作品");
        baseViewHolder.getView(R.id.pao).requestFocus();


        if (dataBean.getItemType() != 1 ) {
            //判断是否关注，如果关注了，将关注图标隐藏

            if(dataBean.getGuanzhu() == 1){
                baseViewHolder.getView(R.id.add_iv).setVisibility(View.GONE);
            }
            //判断是否点赞，如果关注了，将图标红色

            if (dataBean.getDianzan() == 1){
                ImageView view = baseViewHolder.getView(R.id.dz_iv);
                view.setImageResource(R.drawable.ic_xihuanred);
                baseViewHolder.setText(R.id.dz, dataBean.getLike_count()+1 + "");
            }else{
                ImageView view = baseViewHolder.getView(R.id.dz_iv);
                view.setImageResource(R.drawable.ic_xihuan);
                baseViewHolder.setText(R.id.dz, dataBean.getLike_count()-1 + "");
            }
            StoreVideoDap dao = DBUtils.getInstance().getStoreVideoDap();
            ImageView iv2=baseViewHolder.getView(R.id.share_iv);
            if(dao.getById(dataBean.getId())!=null){
                iv2.setImageResource(R.drawable.ic_shoucangyellow);
            }else{
                iv2.setImageResource(R.drawable.ic_shoucang);
            }


            ImageView im = baseViewHolder.getView(R.id.gz_iv);
            ImageView im2 = baseViewHolder.getView(R.id.head_zhuan);


            Glide.with(getContext()).load(dataBean.getHeadpath()).circleCrop().into(im);
            Glide.with(getContext()).load(dataBean.getHeadpath()).circleCrop().into(im2);
            //旋转
            ObjectAnimator rotation = ObjectAnimator.ofFloat(im2, "rotation", 0, 720);
            rotation.setDuration(3000);
            rotation.setRepeatCount(ValueAnimator.INFINITE);
            rotation.setInterpolator(new LinearInterpolator());
            rotation.start();
        }
    }

}
